package cn.core.sign.rest;

import cn.core.sign.eo.Sessions;
import cn.core.sign.qo.SessionsQO;
import cn.core.sign.service.SessionsService;
import cn.core.sign.vo.SessionsVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 会话 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("sign/sessions")
public class SessionsController extends CoreController {

  public static final String name = "会话";

  @Resource
  private SessionsService sessionsService;

  /**
   * 会话 >>> 保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(save)
  public Res<Sessions> save(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.save(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 批量保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(saveBatch)
  public Res<Sessions> saveBatch(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.saveBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(update)
  public Res<Sessions> update(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.update(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 批量修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(updateBatch)
  public Res<Sessions> updateBatch(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.updateBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(remove)
  public Res<Sessions> remove(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      sessionsService.remove(param.get());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 批量删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(removeBatch)
  public Res<Sessions> removeBatch(@RequestBody Param<Sessions> param) throws Exception {
    Res<Sessions> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      sessionsService.removeBatch(param.getData());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(count)
  public Res<Sessions> count(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<Sessions> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(sessionsService.count(null));
      } else {
        res.setCount(sessionsService.count(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pk)
  public Res<Sessions> pk(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<Sessions> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.pk(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(query)
  public Res<Sessions> query(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<Sessions> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(sessionsService.query(null));
      } else {
        res.setData(sessionsService.query(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(paging)
  public Res<Sessions> paging(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<Sessions> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.paging(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 业务数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(countVo)
  public Res<SessionsVO> countVo(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<SessionsVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(sessionsService.countVo(null));
      } else {
        res.setCount(sessionsService.countVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 业务主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pkVo)
  public Res<SessionsVO> pkVo(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<SessionsVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.pkVo(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 业务查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(queryVo)
  public Res<SessionsVO> queryVo(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<SessionsVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(sessionsService.queryVo(null));
      } else {
        res.setData(sessionsService.queryVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 会话 >>> 业务分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(pagingVo)
  public Res<SessionsVO> pagingVo(@RequestBody Param<SessionsQO> param) throws Exception {
    Res<SessionsVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(sessionsService.pagingVo(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
